/*
 * ﻿Copyright (C) 2009-2013 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.gwetons.logging.client;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.logging.client.ConsoleLogHandler;
import com.google.gwt.logging.client.DevelopmentModeLogHandler;
import com.google.gwt.logging.client.FirebugLogHandler;
import com.google.gwt.logging.client.SystemLogHandler;
import com.newmainsoftech.gwetons.logging.shared.LoggerInitializerForTest;

class LoggerInitializerForTestImpl implements LoggerInitializerForTest {
	private LoggerInitializerForTestImpl() {
	}
	
	int count = 0;
	
	/**
	 * Set level of root logger to warning level. <br />
	 * Set up the following log handlers:
	 * <ul>
	 * <li>SystemLogHandler</li>
	 * <li>DevelopmentModeLogHandler</li>
	 * <li>FirebugLogHandler</li>
	 * <li>ConsoleLogHandler</li>
	 * </ul>
	 */
	public void setupLogger() {
		if ( count++ > 0) return;
		
		Logger rootLogger = Logger.getLogger( "");
			rootLogger.setLevel( Level.WARNING);
			// Handlers for development mode --------------------------------------------------
			SystemLogHandler systemLogHandler = new SystemLogHandler();
				systemLogHandler.setLevel( Level.ALL);
				rootLogger.addHandler( systemLogHandler);
			DevelopmentModeLogHandler developmentModeLogHandler = new DevelopmentModeLogHandler();
				developmentModeLogHandler.setLevel( Level.ALL);
				rootLogger.addHandler( developmentModeLogHandler);
			// --------------------------------------------------------------------------------
			// Handlers for production mode ---------------------------------------------------
			// Due to GWT add-on for recent FF version is not available, not certain whether FirebugLogHandler works 
			FirebugLogHandler firebugLogHandler = new FirebugLogHandler();
				firebugLogHandler.setLevel( Level.ALL);
				rootLogger.addHandler( firebugLogHandler);
			// ConsoleLogHandler handler doesn't work on recent FF version and Chrome; haven't checked on other browsers.
			ConsoleLogHandler consoleLogHandler = new ConsoleLogHandler();
				consoleLogHandler.setLevel( Level.ALL);
				rootLogger.addHandler( consoleLogHandler);
			// --------------------------------------------------------------------------------
	}
	
	static final LoggerInitializerForTestImpl instance = new LoggerInitializerForTestImpl();
}